﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FixedTextField.cs" company="Marcelo Roca">
//   Copyright (c) 2011 Marcelo Roca. All rights reserved.
// </copyright>
// <author name="Marcelo Roca" email="marce@marceloroca.com"/>
// <license>
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see http://www.gnu.org/licenses/.
// </license>
// --------------------------------------------------------------------------------------------------------------------

namespace GameKits.Unity.GuiForms.Controls
{
    using System;
    using UnityEngine;

    /// <summary>
    /// Make a single-line text field where the user can edit a string.
    /// </summary>
    public class FixedTextField : FixedControl
    {
        /// <summary>
        /// El tamaño máximo de la cadena
        /// </summary>
        private int maxLenght = int.MaxValue;

        /// <summary>
        /// Se ejecuta cuando se modifica el texto
        /// </summary>
        public event EventHandler TextChanged;

        /// <summary>
        /// Se ejecuta cuando se inmtrodude una letra
        /// </summary>
        public event EventHandler<KeyEventArgs> KeyPressed;

        /// <summary>
        /// Gets or sets Text to edit. 
        /// </summary>
        public string Text
        {
            get { return this.Content.text; }

            set { this.Content.text = value; }
        }

        /// <summary>
        /// Gets or sets MaxLenght.
        /// </summary>
        /// <remarks>
        /// The maximum length of the string. If left out, the user can type for ever and ever.
        /// </remarks>
        public int MaxLenght
        {
            get { return this.maxLenght; }

            set { this.maxLenght = Mathf.Clamp(value, 0, int.MaxValue); }
        }

        /// <summary>
        /// Gets or sets a value indicating whether ReadOnly.
        /// </summary>
        public bool ReadOnly { get; set; }

        /// <summary>
        /// Pintamos el control
        /// </summary>
        protected override void DrawControl()
        {
            string txt;

            if (this.Style != null)
            {
                txt = GUI.TextField(this.PositionRect, this.Text, this.MaxLenght, this.Style);
            }
            else
            {
                txt = GUI.TextField(this.PositionRect, this.Text, this.MaxLenght);
            }

            if (!this.ReadOnly)
            {
                if (txt != this.Text)
                {
                    this.Text = txt;

                    if (this.TextChanged != null)
                    {
                        this.TextChanged(this, new EventArgs());
                    }
                }

                if (Event.current.isKey)
                {
                    if (this.KeyPressed != null)
                    {
                        this.KeyPressed(this, new KeyEventArgs { KeyPressed = Event.current.keyCode, Control = Event.current.control, Shift = Event.current.shift, Alt = Event.current.alt });
                    }
                }
            }
        }
    }
}
